package services

import (
	"context"
	"github.com/golang/protobuf/ptypes/empty"
	"github.com/golang/protobuf/ptypes/wrappers"
	"kop/modules/award/models"
	"kop/pb"

	captainC "kop/modules/captain/client"
	questC "kop/modules/quest/client"
	roleC "kop/modules/role/client"
)

// 所有事件
func (s *RoleService) GetEvents(ctx context.Context, req *empty.Empty) (*pb.Int32Slice, error) {

	var reply pb.Int32Slice
	var roleID = s.GetRoleID(ctx)

	reply.Slices = roleC.GetEvents(roleID)
	return &reply, nil
}

// 处理事件类型1
func (s *RoleService) Event1(ctx context.Context, req *wrappers.Int32Value) (*pb.RoleEventReply, error) {

	var reply pb.RoleEventReply
	var roleID = s.GetRoleID(ctx)

	var res, err = roleC.Event1(roleID, req.Value)
	if err != nil {
		return &reply, err
	}

	var role = roleC.GetRole(roleID)

	reply.EventIDs = res.EventIDs
	reply.Success = true
	reply.Award = models.RandAward(role, res.AwardID)

	// quest , daily event
	go questC.Event(roleID, s.GetServerID(ctx))

	return &reply, models.SendAward(role, reply.Award)
}

// 处理事件类型2
func (s *RoleService) Event2(ctx context.Context, req *pb.RoleEvent2Request) (*pb.RoleEventReply, error) {

	var reply pb.RoleEventReply
	var roleID = s.GetRoleID(ctx)

	var res, err = roleC.Event2(roleID, req.EventID, req.Answer)
	if err != nil {
		return &reply, err
	}

	var role = roleC.GetRole(roleID)

	reply.EventIDs = res.EventIDs
	reply.Success = true
	reply.Award = models.RandAward(role, res.AwardID)

	// quest , daily event
	go questC.Event(roleID, s.GetServerID(ctx))

	return &reply, models.SendAward(role, reply.Award)
}

// 处理事件类型3
func (s *RoleService) Event3(ctx context.Context, req *pb.RoleEvent3Request) (*pb.RoleEventReply, error) {

	var reply pb.RoleEventReply
	var roleID = s.GetRoleID(ctx)

	var captain = captainC.GetCaptain(roleID, req.CaptainID)

	var res, err = roleC.Event3(roleID, req.EventID, captain)
	if err != nil {
		return &reply, err
	}

	var role = roleC.GetRole(roleID)

	reply.EventIDs = res.EventIDs
	reply.Success = true
	reply.Award = models.RandAward(role, res.AwardID)

	// quest , daily event
	go questC.Event(roleID, s.GetServerID(ctx))

	return &reply, models.SendAward(role, reply.Award)
}

func (*RoleService) GetVIPAward(ctx context.Context, req *wrappers.Int32Value) (*pb.Award, error) {

	return roleC.RoleService.GetVIPAward(ctx, req)
}
